//
//  HPDrawAlertView.m
//  HPZZTong
//
//  Created by panbin on 13-11-25.
//  Copyright (c) 2013年 Handpay. All rights reserved.
//

#import "HPDrawAlertView.h"
#import "DefineUtil.h"
#import "HPSignView.h"
#import "ZZTongDefine.h"
/**
 * 弹出框的高度
 */
#define HP_DRAW_ALERT_HEIGHT 300

/**
 * 手写框的高度
 */
#define HP_WRITE_ALERT_HEIGHT 200

@interface HPDrawAlertView()

@property (nonatomic, retain) UIButton *cancelButton;
@property (nonatomic, retain) HPSignView *signView;
@property (nonatomic, retain) UILabel* tipLabel;

@end

@implementation HPDrawAlertView


/**
 * 保持图片
 */
- (UIImage*)getImage {
//    if ([_signView isDrawedInScreen]) {
        UIGraphicsBeginImageContext(_signView.frame.size);
        
        [_signView.layer renderInContext:UIGraphicsGetCurrentContext()];
        
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
//        if ((_delegate) && ([_delegate respondsToSelector:@selector(drawAlertView:didSaveImage:isSign:)])) {
//            [_delegate drawAlertView:self didSaveImage:image isSign:[_signView isDrawedInScreen]];
//        }
        return image;
//    }
    return nil;
}

/**
 * 清除画线
 */
- (void)buttonCancel {
    [_signView doClearScreen];
    _tipLabel.hidden = NO;
//     _tipLabel.text = @"请顾客在此签名";
    [NOTIFICATION postNotificationName:HPStartDrawNotificationKey object:[NSNumber numberWithBool:NO]];
}

- (void)updateUI:(NSNotification*)note{
    NSNumber* numberValue = [note object];
    BOOL isDraw = numberValue.boolValue;
    if (isDraw) {
        _tipLabel.hidden = isDraw;
    }
    
//    _tipLabel.text = @"";
}
- (void)createSignView{
    CGRect frame = self.frame;
    //画签名的类
    HPSignView* sign = [[HPSignView alloc] initWithFrame:CGRectMake(3,  3, frame.size.width - 6, frame.size.height - 6)];
    sign.backgroundColor = [UIColor whiteColor];
    [self addSubview:sign];
    self.signView = sign;
    [sign release];
    
    
    CGRect tipFrame = frame;
    tipFrame.size.height -= 42;
    UILabel* tip = [[UILabel alloc]initWithFrame:tipFrame];
    tip.text = @"持卡人签字";
    tip.textAlignment = NSTextAlignmentCenter;
    tip.font= kMaxAmountFont;
    tip.textColor = kGrayFontColor;
    
    [self addSubview:tip];
    self.tipLabel = tip;
    [tip release];
    
    //清除按钮
    UIButton* cancel = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width - 70, frame.size.height - 50, 60, 40)];
    [cancel setBackgroundImage:[[UIImage imageNamed:@"whitebutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateNormal];
    [cancel setBackgroundImage:[[UIImage imageNamed:@"whitebuttonhover.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateHighlighted];
    [cancel setTitleColor:kNormalFontColor forState:UIControlStateNormal];
    [cancel setTitleColor:kNormalFontColor forState:UIControlStateHighlighted];
    [cancel setTitle:@"重写" forState:UIControlStateNormal];
    [cancel setTitle:@"重写" forState:UIControlStateHighlighted];
    [cancel addTarget:self action:@selector(buttonCancel) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancel];
    self.cancelButton = cancel;
    [cancel release];
}
- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [NOTIFICATION addObserver:self selector:@selector(updateUI:) name:HPStartDrawNotificationKey object:nil];
        [self createSignView];
    }
    
    return self;
}

- (void)dealloc {
    [NOTIFICATION removeObserver:self];
    self.cancelButton = nil;
    self.signView = nil;
    self.tipLabel = nil;
    [super dealloc];
}

@end
